package com.data.action;

import com.data.bean.Account;
import com.data.entity.ActiveEmail;
import com.data.entity.TempMassage;
import com.data.service.FillMassageService;
import com.data.util.AddAccountUtils;
import com.data.util.CheckDataUtils;
import com.data.util.FinalStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 完善注册信息相关
 */
@Controller
@RequestMapping("/android/fillMassageAction")
public class FillMassageAction {
    @Autowired
    private FillMassageService fillMassageService;
    @Autowired
    private AddAccountUtils addAccountUtils;

    /**
     * 根据类型进入到不同选择身份界面
     *
     * @param type
     * @return
     */
    @GetMapping(value = "/input")
    public String inputMassgeAction(@RequestParam("type") Integer type, HttpServletRequest request) {
        Account account = (Account) request.getSession().getAttribute("Account");
        if (account.getTypes() == 0) {
            request.setAttribute("type", type);
            if (type == FinalStringUtils.PERSON) {
                return "/WEB-INF/login/person";
            } else if (type == FinalStringUtils.COMPANY) {
                return "/WEB-INF/login/company";
            } else {
                return "404";
            }
        } else {
            return "redirect:/";
        }

    }

    /**
     * 进入到身份选择界面
     *
     * @return
     */
    @GetMapping("choiceIdentity")
    public String inputchoiceAction() {

        return "/WEB-INF/login/choiceIdentity";
    }

    /**
     * 提交完善的信息
     *
     * @param filename
     * @param massage
     * @return
     */

    @PostMapping(value = "/submitMassage")
    public String submitMassageAction(@RequestParam("filename") MultipartFile filename, TempMassage massage, HttpServletRequest request, Integer type) throws IOException {
        //校验上传数据的完整性

        if (CheckDataUtils.checkAccountType(type)) {
            if (filename != null && massage != null && CheckDataUtils.stringUtils(massage.getNumber(), massage.getName(), type == 2 ? massage.getCname() : massage.getName())) {
                //校验邮箱是否正确
                if (CheckDataUtils.checkEmail(massage.getEmail())) {
                    Account account = (Account) request.getSession().getAttribute("Account");
                    String uid = account.getId();
                    account.setTypes(type);
                    request.setAttribute("Account", account);
                    //选择身份进入
                    if (fillMassageService.choiceAccountService(uid, type)) {
                        boolean bool;
                        if (FinalStringUtils.COMPANY == type) {
                            bool = fillMassageService.saveMassageCompanyService(filename, massage, uid);
                        } else{
                            bool = fillMassageService.saveMassagePersonService(filename, massage, uid);
                        }

                        if (bool) {
                            request.setAttribute("id", uid);
                            request.setAttribute("email", massage.getEmail());
                            request.setAttribute("type", type);
                            return "/WEB-INF/login/massageEmail";
                        } else {
                            return "404";
                        }

                    } else {
                        return "404";
                    }
                }

                return "404";
            }
        }

        return "404";
    }

    /**
     * 邮箱激活
     *
     * @param activeEmail
     * @param request
     * @return
     */
    @GetMapping(value = "/activeEmail")
    public String activeEmailAction(ActiveEmail activeEmail, HttpServletRequest request) {
        boolean bool = fillMassageService.activeEmailService(activeEmail);
        if (bool) {
            //注册信息成功，直接添加回话到session中
            addAccountUtils.addAccountSession(request, activeEmail.getId());
            //创建应用的界面
            return "/WEB-INF/login/createApp";
        } else {
            //邮箱验证失败
            return "/WEB-INF/login/emailInvalid";
        }

    }


    /**
     * 重发激活码
     *
     * @param id
     * @param email
     * @param type
     * @return
     */
    @PostMapping("reSendMail")
    @ResponseBody
    public String reSendMailAction(String id, String email, int type) {
        boolean bool = fillMassageService.reSendMailService(id, email, type);
        return String.valueOf(bool);
    }

    /**
     * @param id
     * @param email
     * @param type
     * @return
     */
    @PostMapping("changeEmail")
    @ResponseBody
    public String changeMailAction(String id, String email, int type) {
        boolean bool = fillMassageService.changeMailService(id, email, type);
        return String.valueOf(bool);
    }

}
